package com.alipay.app.groupmeal.standard.ui.b.function.mode;

import android.content.Context;
import android.text.Editable;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;

import androidx.annotation.Nullable;

import com.alipay.app.groupmeal.standard.R;
import com.alipay.app.groupmeal.standard.ui.b.util.DeviceInfoManager;
import com.alipay.app.groupmeal.standard.widget.ActionButton;
import com.alipay.app.groupmeal.standard.widget.InputNumberView;
import com.alipay.app.groupmeal.standard.widget.business.BaseBScreenView;

public class BScreenSetExpireTimeDialog extends BaseBScreenView {

    private InputNumberView tv_amount;

    private AmountConfirmListener listener;
    private ActionButton mBtnAction;
    private boolean isSetting = false;

    public BScreenSetExpireTimeDialog(Context context) {
        super(context);
    }

    public BScreenSetExpireTimeDialog(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.view_set_success_time_dialog;
    }

    @Override
    protected void initView(View view) {
        tv_amount = view.findViewById(R.id.tv_amount);
        mBtnAction = view.findViewById(R.id.btn_action);
    }

    @Override
    protected void initData() {
        tv_amount.addTextChangedListener(new InputNumberView.CustomTextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (isSetting) {
                    if (tv_amount.getText().length() > 0) {
                        mBtnAction.setVisibility(VISIBLE);
                    } else
                        mBtnAction.setVisibility(INVISIBLE);
                }
            }
        });
    }

    @Override
    public boolean onKeyEventEnter(KeyEvent event) {
        if (tv_amount.getText().length() > 0) {
            if (isSetting) {
                DeviceInfoManager.setFixedAmount(tv_amount.getText());
                setVisibility(GONE);
            }
            if (listener != null) {
                listener.onConfirm(tv_amount.getText());
            }
        }
        return super.onKeyEventEnter(event);
    }

    @Override
    public boolean onKeyEventBack(KeyEvent event) {
        dismiss();
        return super.onKeyEventEnter(event);
    }

    @Override
    public boolean onKeyEventNum(KeyEvent event) {
        if (tv_amount != null) {
            return tv_amount.dispatchKeyEvent(event);
        }
        return super.onKeyEventNum(event);
    }

    public void setAmountConfirmListener(AmountConfirmListener amountConfirmListener) {
        listener = amountConfirmListener;
    }

    public interface AmountConfirmListener {
        void onConfirm(String amount);
    }

    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(visibility);
        if (visibility == View.VISIBLE) {
            tv_amount.setText("");
        }
    }

    public void setSetting(boolean setting) {
        isSetting = setting;
    }

    public InputNumberView getTv_amount() {
        return tv_amount;
    }
}
